﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml.Serialization;

//using EPiServer.Security;

namespace Cognifide.PowerShell.Shell.Storage
{
    [Serializable]
    public class ContextScriptRepository
    {

        public static string[] FileNames
        {
            get
            {
                return Directory.GetFiles(HttpContext.Current.Request.PhysicalApplicationPath, "*.psepi", SearchOption.AllDirectories);
            }
        }

        static ContextScriptRepository Instance
        {
            get
            {
                if (instance == null || lastRefresh < DateTime.Now)
                {
                    lastRefresh = DateTime.Now.AddMinutes(1);
                    instance = Load();
                }
                return instance;
            }
        }

        public ContextScriptRepository()
        {
            ScriptCollections = new List<ContextScriptCollection>();
        }

        public string Title;
        public string Description;
        public List<ContextScriptCollection> ScriptCollections { get; private set; }

        private static DateTime lastRefresh;
        private static ContextScriptRepository instance;
     
/*
        public ContextScriptRepository GetForPrincipal(PrincipalInfo principal, ScriptArea scriptArea)
        {            
            ContextScriptRepository result = (ContextScriptRepository) this.MemberwiseClone();
            result.ScriptCollections = new List<ContextScriptCollection>();
            foreach (var scriptCollection in (ScriptCollections))
            {
                if (scriptArea == scriptCollection.Area)
                {
                    ContextScriptCollection collection = scriptCollection.GetForPrincipal(principal);
                    if (collection.Scripts.Count > 0)
                    {
                        result.ScriptCollections.Add(collection);
                    }
                }
            }
            return result;
        }
*/

        public static ContextScriptRepository Load()
        {
            ContextScriptRepository result = new ContextScriptRepository();
            foreach (string fileName in FileNames)
            {
                if (File.Exists(fileName))
                {
                    using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof (ContextScriptCollection));
                        result.ScriptCollections.Add(serializer.Deserialize(fileStream) as ContextScriptCollection);
                    }
                }
            }
            return result;
        }

        public ContextScript FindScriptByName(string scriptTitle)
        {
            foreach (var collection in ScriptCollections)
            {
                foreach (var script in collection.Scripts)
                {
                    if (script.Title == scriptTitle)
                    {
                        return script;
                    }
                }
            }
            return null;
        }
    }
}